#include "timer.h"
#include "led.h"

extern u_int rank;

int flag = 0;

void TIM3_Init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  //定义了定时器的结构体
	NVIC_InitTypeDef NVIC_InitStructure;

	//时钟频率84MHz
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能 TIM3 时钟

	TIM_TimeBaseStructure.TIM_Period = 1000;  //自动重装载值，计数值
	TIM_TimeBaseStructure.TIM_Prescaler =8400; //预分频，实际使用的时钟频率84MHz/84=1MHz 84MHz/8400 = 10000Hz
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //分频因子为1
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
	
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
	
	//进行NVIC中断配置
	NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //定时器 3 中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级 1
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //响应优先级 3
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);// ④初始化 NVIC
	
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //③允许定时器 3 更新中断

	TIM_Cmd(TIM3,ENABLE); //⑤使能定时器 3
}

void TIM3_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中断
	{
		if(flag==0)
		{
			rank+=49;
			if(rank>=980)
				flag = 1;
		}
		else
		{
			rank-=49;
			if(rank<=0)
				flag = 0;
		}
		/*处理的程序段*/
		//GPIO_ToggleBits(GPIOF, GPIO_Pin_9);
		
	}
	TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除中断标志位


}
